<template>
    <div class="header">
        <a href="/" class="logo" style="margin-left: 15px;color: #FFF;">
            <!--<img src="../../../static/img/logo.png" width="30px"  alt="">-->
            <i class="el-icon-lx-home"></i>
            佳颖业务管理平台
        </a>
        <div class="header-right">
            <div class="header-user-con">
                <!-- 用户名下拉菜单 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        <i class="el-icon-lx-people"></i>
                        {{username}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item divided  command="loginout">
                            <i class="el-icon-lx-exit"></i>
                            退出登录
                        </el-dropdown-item>
                        <el-dropdown-item divided  command="changpassword">
                            <i class="el-icon-key"></i>
                            修改密码
                        </el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </div>
        <el-dialog title="修改密码" :visible.sync="isDialog1" width="500px" top="50px" :close-on-click-modal="false">
            <el-form ref="dialogForm" :model="dialogForm" label-width="100px" :rules="rules">
                <el-form-item label="新密码" prop="password">
                    <el-input placeholder="请输入密码" v-model="dialogForm.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="aginPassword">
                    <el-input placeholder="请输入密码" v-model="dialogForm.aginPassword" show-password></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="changePassword">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
    import bus from '../common/bus';
    import baseHttp from '@/axios/baseHttp.js'
    export default {
        data() {
            return {
                collapse: false,
                fullscreen: false,
                name: 'linxin',
                message: 2,
                dialogForm:{},
                isDialog1:false,
                rules:{
                    password:[{ required: true, message: '请输入新密码', trigger: 'blur' }],
                    aginPassword:[{ required: true, message: '请再次确认密码', trigger: 'blur' }],
                }
            }
        },
        computed:{
            username(){
                let username = localStorage.getItem('ms_username');
                return username ? username : this.name;
            }
        },
        methods:{
            // 用户名下拉菜单选择事件
            handleCommand(command) {
                let self = this;
                let _token = sessionStorage.getItem("tokenId");
                if(command === 'loginout'){
                    baseHttp.loginOut().then(res=>{
                        sessionStorage.removeItem('username');
                        localStorage.removeItem('ms_username');
                        self.$router.push('/login');
                    }).catch(function (catchres) {
                        sessionStorage.removeItem('username');
                        localStorage.removeItem('ms_username');
                        self.$router.push('/login');
                    })
                }
                if(command === 'changpassword'){
                    self.isDialog1 = true;
                }
            },
            changePassword(){
                /**修改密码 */
                let self = this;
                self.$refs['dialogForm'].validate((valid) => {
                    if (valid) {
                       if(self.dialogForm.password===self.dialogForm.aginPassword){
                           let userId = sessionStorage.getItem("userId");
                            baseHttp.changePassword(userId,self.dialogForm.password).then(res=>{
                                self.isDialog1 = false;
                                self.$message({
                                    message: '密码修改成功',
                                    type: 'success',
                                    onClose:()=>{
                                        self.$router.push('/login')
                                    }
                                });
                            })
                       }else{
                           self.$message.error(`两次输入的密码不一致`)
                       }
                    }
                });
            }
        }
    }
</script>
<style scoped>
    .header {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #fff;
    }
    .header .logo{
        float: left;
        width:250px;
        line-height: 70px;
    }
    .header-right{
        float: right;
        padding-right: 50px;
    }
    .header-user-con{
        display: flex;
        height: 70px;
        align-items: center;
    }
    .btn-bell .el-icon-bell{
        color: #fff;
    }
    .user-name{
        margin-left: 10px;
    }
    .user-avator{
        margin-left: 20px;
    }
    .user-avator img{
        display: block;
        width:40px;
        height:40px;
        border-radius: 50%;
    }
    .el-dropdown-link{
        color: #fff;
        cursor: pointer;
    }
    .el-dropdown-menu__item{
        text-align: center;
    }
</style>
